import { createWebHistory, createRouter } from 'vue-router'
import Index from '../views/Index.vue'
import Home from '../views/Home.vue'
import About from '../views/About.vue'
import Users from '../views/Users.vue'
//import UserDetail from '../views/UserDetail.vue'
//懒加载
const UserDetail = ()=>import('../views/UserDetail.vue')

//配置video相关路由
const Video = {
    path:'/video',
    component:()=>import('../views/video/Index.vue'),
    name:'videoList',
    children:[
        {path:'movie',component:()=>import('../views/video/Movie.vue')},
        {path:'tv',component:()=>import('../views/video/Tv.vue')},
    ]
}

//定义映射关系
const routes = [
    Video,
    {path:'/',component:Index},
    {path:'/home',component:Home,beforeEnter:(to,form,next)=>{
        next()
    }},
    {path:'/about',component:About},
    {path:'/users/:id',component:Users},
    {path:'/userDetail/:id/:username',component:UserDetail,name:'userDetail',beforeEnter:(to,form,next)=>{
        if(to.params.id == 1){
            next()
        }else{
            next('/')
        }
    }},
]

//创建router实例，并制定历史记录模式
const router = createRouter({
    history: createWebHistory(),
    routes,
})


router.beforeEach((to,form,next)=>{
    console.log("to:",to)
    console.log("form:",form)
    //console.log("next:",next)
    if(to.query.token != '123' && to.path == '/video'){
        next('/')    
    }else{
        next()
    }
})

export default router